//
//  PowerDetailCell.swift
//  SCA
//
//  Created by tjun on 2025/9/13.
//

import UIKit

struct PowerDetailModel {
    let Id: Int
    let durationDisp: String
    let statusDisp: String
    let aprDisp: String
    let status: Int
    let reward: String
    let completeness: String
    let investmentDisp: String
    let walletAmountDisp: String
    let rewardDisp: String
    let completenessDisp: String
    let startDate: String
    let endDate: String
    let createDate: String
    let duration: Int
    let orderId: String
    let pool: String
    
    init(dict: [String: Any]) {
        self.Id = dict.intValue("id")
        self.status = dict.intValue("status")
        self.duration = dict.intValue("duration")
        self.durationDisp = dict.stringValue("duration_disp")
        self.aprDisp = dict.stringValue("apr_disp")
        self.reward = dict.stringValue("reward")
        self.completeness = dict.stringValue("completeness")
        self.investmentDisp = dict.stringValue("investment_disp")
        self.walletAmountDisp = dict.stringValue("wallet_amount_disp")
        self.rewardDisp = dict.stringValue("reward_disp")
        self.startDate = dict.stringValue("sdate")
        self.createDate = dict.stringValue("cdate")
        self.endDate = dict.stringValue("edate")
        self.orderId = dict.stringValue("orderid")
        self.pool = dict.stringValue("name")
        self.statusDisp = dict.stringValue("status_disp")
        self.completenessDisp = dict.stringValue("completeness_disp")
    }
    
}

class PowerDetailCell: BaseTableViewCell {
    
    
    private let titleLabel = UILabel(font: UIFontSemiboldMake(16), textColor: .white)
    private let dayLabel = GradientBorderLabel(font: UIFontMediumMake(12), textColor: .white, textAlignment: .center)
    private let dateLabel = UILabel(font: UIFontMediumMake(12), textColor: .white)
    private let startDateLabel = UILabel(font: UIFontMake(12), textColor: .white)
    private let endDateLabel = UILabel(font: UIFontMake(12), textColor: .white)
    private let yearInterestRateValueLabel = UILabel(font: UIFontSemiboldMake(15), textColor: .white)
    private let pledgeValueValueLabel = UILabel(font: UIFontSemiboldMake(15), textColor: .white)
    private let incomeValueLabel = UILabel(font: UIFontSemiboldMake(15), textColor: .white)
    private let progressLabel = UILabel(font: UIFontMake(15), textColor: .white, textAlignment: .center)
    private let progressView = GradientCircularProgressView()
    private let stateLabel = UILabel(font: UIFontMake(15), textColor: .white, textAlignment: .center)

    var model: PowerDetailModel! {
        didSet {
            
            titleLabel.text = model.orderId
            dayLabel.text = model.durationDisp
            
            dateLabel.text = model.createDate
           
            startDateLabel.attributedText = String(format: "开始日期：%@".localizedString, model.startDate).attributedString(rangeString: model.startDate, rangeColor: UIColorMake(0x8F9297))
            
            if !model.endDate.isBlank(), model.endDate != "-" {
                endDateLabel.attributedText = String(format: "结束日期：%@".localizedString, model.endDate).attributedString(rangeString: model.endDate, rangeColor: UIColorMake(0x8F9297))
            } else {
                endDateLabel.attributedText = nil
            }
            
            progressLabel.text = String(format: "完成度 %@".localizedString, model.completenessDisp)
            progressView.progress = CGFloat((model.completeness as NSString).doubleValue)
            
            pledgeValueValueLabel.text = model.investmentDisp
            incomeValueLabel.text = model.rewardDisp
            yearInterestRateValueLabel.text = model.aprDisp

            stateLabel.text = model.statusDisp

            if model.status == 1 {
                stateLabel.textColor = UIColorMake(0xDFDFDF)
                stateLabel.backgroundColor = UIColorMake(0xA6A9AF)
            } else {
                stateLabel.backgroundColor = UIColor.white.withAlphaComponent(0.8)
                stateLabel.textColor = UIColor.black
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = AppStyle.color.background
        selectionStyle = .none
        
        let backgroundContainerView = UIView()
        backgroundContainerView.addCorner(radius: 10)
        backgroundContainerView.backgroundColor = UIColorMake(0x2E323F)
        contentView.addSubview(backgroundContainerView)
        backgroundContainerView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(20)
            make.top.equalToSuperview().offset(16)
            make.bottom.equalToSuperview()
        }
        
        let titleView = UIView().addTo(backgroundContainerView)
        titleView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalToSuperview()
            make.height.equalTo(48)
        }
        
        let titleLineView = UIView().addTo(titleView)
        titleLineView.backgroundColor = .white.withAlphaComponent(0.3)
        titleLineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(12)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        titleLabel.addTo(titleView)
        titleLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalToSuperview()
        }
        
        dayLabel.addTo(titleView)
        dayLabel.backgroundColor = UIColor.black
        dayLabel.gradientColors = [UIColorMake(0x05D4FC), UIColorMake(0xBE0DEF), UIColorMake(0xF202D3)]
        dayLabel.borderWidth = 2
        dayLabel.layer.cornerRadius = 14
        dayLabel.layer.masksToBounds = true
        dayLabel.snp.makeConstraints { make in
            make.left.equalTo(titleLabel.snp.right).offset(10)
            make.centerY.equalTo(titleLabel)
            make.width.equalTo(60)
            make.height.equalTo(26)
        }
        
        dateLabel.addTo(titleView)
        dateLabel.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.centerY.equalToSuperview()
        }
        
        let startEndDateView = UIView().addTo(backgroundContainerView)
        startEndDateView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleView.snp.bottom)
            make.height.equalTo(40)
        }
        
        startDateLabel.addTo(startEndDateView)
        startDateLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.centerY.equalToSuperview()
        }
        
        endDateLabel.addTo(startEndDateView)
        endDateLabel.snp.makeConstraints { make in
            make.right.equalTo(-12)
            make.centerY.equalToSuperview()
        }
        
        let startEndDateLineView = UIView().addTo(startEndDateView)
        startEndDateLineView.backgroundColor = .white.withAlphaComponent(0.3)
        startEndDateLineView.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(12)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
        }
        
        let infoView = UIView().addTo(backgroundContainerView)
        infoView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(startEndDateLineView.snp.bottom)
            make.bottom.equalToSuperview()
        }
        
        let yearInterestRateLabel = UILabel(text: "年利率".localizedString, font: UIFontMediumMake(16), textColor: .white)
        yearInterestRateLabel.addTo(infoView)
        yearInterestRateLabel.snp.makeConstraints { make in
            make.left.equalTo(12)
            make.top.equalTo(18)
            make.width.equalTo(80)
        }
        
        yearInterestRateValueLabel.addTo(infoView)
        yearInterestRateValueLabel.snp.makeConstraints { make in
            make.left.equalTo(yearInterestRateLabel.snp.right).offset(10)
            make.centerY.equalTo(yearInterestRateLabel)
        }
        
        let pledgeValueLabel = UILabel(text: "质押价值".localizedString, font: UIFontMediumMake(16), textColor: .white).addTo(infoView)
        pledgeValueLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.width.equalTo(yearInterestRateLabel)
        }
        
        pledgeValueValueLabel.addTo(infoView)
        pledgeValueValueLabel.snp.makeConstraints { make in
            make.centerY.equalTo(pledgeValueLabel)
            make.left.equalTo(yearInterestRateValueLabel)
        }
        
        let incomeLabel = UILabel(text: "收益".localizedString, font: UIFontMediumMake(16), textColor: .white).addTo(infoView)
        incomeLabel.snp.makeConstraints { make in
            make.bottom.equalTo(-18)
            make.left.width.equalTo(yearInterestRateLabel)
        }
        
        incomeValueLabel.addTo(infoView)
        incomeValueLabel.snp.makeConstraints { make in
            make.centerY.equalTo(incomeLabel)
            make.left.equalTo(yearInterestRateValueLabel)
        }
        
        stateLabel.addTo(infoView)
        stateLabel.backgroundColor = .lightGray
        stateLabel.addCorner(radius: 10)
        stateLabel.snp.makeConstraints { make in
            make.top.equalTo(20)
            make.right.equalTo(endDateLabel)
            make.width.equalTo(100)
            make.height.equalTo(30)
        }
        
        progressView.addTo(infoView)
        progressView.gradientColors = [UIColorMake(0x05D4FC), UIColorMake(0xBE0DEF), UIColorMake(0xF202D3)]
        progressView.lineWidth = 4
        progressView.snp.makeConstraints { make in
            make.right.equalTo(endDateLabel)
            make.centerY.equalTo(incomeLabel)
            make.width.height.equalTo(30)
        }
        
        progressLabel.addTo(infoView)
        progressLabel.snp.makeConstraints { make in
            make.centerY.equalTo(incomeLabel)
            make.right.equalTo(progressView.snp.left).offset(-20)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
